<?php

namespace App\Library\Traits\Logic;

use App\Library\Logic\ShopImageLogic;
use App\Library\Logic\ShopProductSpecsLogic;
use App\Library\Requests\Product\ProductAddRequest;
use Illuminate\Database\Eloquent\Model;

trait ShopProductLogicTrait
{
    /**
     * hImg
     * @param array $array
     * @param int $userId
     * @param int $id
     * @return array
     */
    private static function hImg(mixed $array, int $userId, int $id): array
    {
        if (is_null($array)) $array = [];
        else if (is_string($array) && strlen($array) === 0) $array = [];
        else if ($array === '[]') $array = [];
        else $array = json_decode($array, true);
        $ids = [];
        foreach ($array as $v) {
            $shopImage = ShopImageLogic::detail($v);
            unsetObject($shopImage, ['img_id', 'serial_number', 'img_source_id', 'created_at', 'updated_at']);
            $nv = ShopImageLogic::insertForModel(array_merge($shopImage->toArray(), [
                'userMainId' => $userId,
                'imgSourceId' => $id,
            ]));
            $ids[] = $nv->img_id;
        }
        return $ids;
    }

    /**
     * unsetProduct
     * @param Model $self
     * @return void
     */
    private static function unsetProduct(Model $self): void
    {
        unsetObject($self, [
            'product_id',
            'serial_number',
            'user_id',
            'product_status',
            'add_user_id',
            'edit_user_id',
            'product_online_time',
            'product_offline_time',
            'product_effective_day',
            'product_effective_time',
            'product_hits',
            'product_total_sell',
            'ps_id_json',
            'spec_json',
            'status_recommend',
            'created_time',
            'created_at',
            'updated_at',
            'deleted_at',
            'add_at',
            'product_avatar_img_id_json',
            'product_content_img_id_json',
            'publish_at',
            'sales',
            'operated_at',
            'status_recommend',
        ]);
    }

    /**
     * saveCom
     * @param ProductAddRequest $request
     * @return array
     */
    private function saveCom(ProductAddRequest $request): array
    {
        $array = handleRequest($request);
        foreach ($array as $k => $v) {
            if (!in_array($k, ['specJson', 'mainImgIds', 'descImgIds', 'videoId'])) {
                $array['product' . ucfirst($k)] = $v;
                unset($array[$k]);
            }
        }
        if ($array['productStatus'] === 2) {
            $array['publishAt'] = now();
            $array['productOnlineTime'] = now()->timestamp;
            $array['createdTime'] = now()->toDateString();
        }
        $array['editUserId'] = USERID;
        $array['productAvatar'] = $request->mainImgIds[0];
        $array['operatedAt'] = now();
        if (!empty($array['specJson'])) {
            foreach ($array['specJson'] as $v){
                $arr[] = (int)cache('shopProductSpec:' . $v);
            }
            $array['psIdJson'] = empty($arr) ? null : json_encode($arr);
        }
        return $array;
    }
}
